package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.*;
import com.suyuan.entity.*;
import com.suyuan.service.ProductService;
import com.suyuan.vo.FeedingInfo;
import com.suyuan.vo.HotProductVO;
import com.suyuan.vo.ProductVO;
import com.suyuan.vo.SourceInfo;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by yang on 2018/3/31.
 */
@Service(value = "productService")
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProducetypeInfoMapper typeMapper;
    @Resource
    private ProductInfoMapper productMapper;
    @Resource
    private RecommProductMapper recommProductMapper;
    @Resource
    private ProPicMapper proPicMapper;
    @Resource
    private ProInspectInfoMapper proInspectInfoMapper;
    @Resource
    private IllnessprocessMapper illnessprocessMapper;
    @Resource
    private PurchaseInfoMapper purchaseInfoMapper;
    @Resource
    private FeedingInforMapper feedingInforMapper;
    @Resource
    private FeedsMapper feedsMapper;
    @Resource
    private CompanyInfoMapper companyMapper;



    public List<ProducetypeInfo> categoryList() {
        return typeMapper.selectByExample(new ProducetypeInfoExample());
    }

    public PageInfo findByCategory(Integer id, Page page) {
        ProductInfoExample example = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = example.createCriteria();
        criteria.andTypeidEqualTo(id);
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<ProductInfo> list = productMapper.selectByExample(example);
        PageInfo info = new PageInfo(list);
        return info;
    }

    public ProductVO findById(Integer id) {
        if(id == null){
            return null;
        }
        ProductInfo product = productMapper.selectByPrimaryKey(id);
        List<ProPic> smallPic = proPicMapper.getProPic(id,0);//缩略图集合
        List<ProPic> bigPic = proPicMapper.getProPic(id,1);//大图
        return new ProductVO(product,smallPic,bigPic);
    }

    public PageInfo findAll(Page page) {
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<ProductInfo> list = productMapper.selectByExample(new ProductInfoExample());
        PageInfo info = new PageInfo(list);
        return info;
    }

    public List<ProductInfo> findAll() {
        List<ProductInfo> list = productMapper.selectByExample(new ProductInfoExample());
        return list;
    }

    public void insert(ProductInfo product) throws Exception {
        productMapper.insert(product);
    }

    public void update(ProductInfo product) {
        productMapper.updateByPrimaryKey(product);
    }

    public void delete(Integer id) {
        productMapper.deleteByPrimaryKey(id);
    }

    public Integer insertProductReturnId(ProductInfo product) {
        return productMapper.insertProductReturnId(product);
    }

    public PageInfo inquiry(String productName, Integer pageNumber, Integer pageSize) {
        ProductInfoExample example = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = example.createCriteria();

        if(StringUtils.isNotBlank(productName)){
            //表示查询所有商品
            criteria.andProductnameLike("%"+productName+"%");
        }
        if(pageNumber == null){
            pageNumber = 1 ;
        }
        if(pageSize == null){
            pageSize = 10;
        }

        PageHelper.startPage(pageNumber,pageSize);
        List<ProductInfo> productInfos = productMapper.selectByExample(example);
        System.out.println(".....................................product size:"+productInfos.size());
        PageInfo info = new PageInfo(productInfos);
        return info;
    }

    public List<ProductInfo> getRecommentPro() {
        List<Integer> ids = recommProductMapper.findRecomIds();
        List<ProductInfo> products = new ArrayList<ProductInfo>();

        for(Integer id : ids){
            ProductInfo product = productMapper.selectByPrimaryKey(id);
            products.add(product);
        }
        return products;
    }

    public List<ProPic> getProPic(Integer id) {
        List<ProPic> pics = proPicMapper.getProPic(id,null);
        return pics;
    }

    public List<ProPic> getSlidePic() {
        //type = 2为轮播图
        List<ProPic> pics = proPicMapper.getProPic(null, 2);
        return pics;
    }

    /**
     * 获取首页人们商品推荐
     * @return
     */
    public List<HotProductVO> getHotProduct() {
        List<HotProductVO> result = new ArrayList<HotProductVO>();

        List<HotProduct> hotProducts = productMapper.getHotProduct();
        for(HotProduct hot : hotProducts){
            ProPic proPic = proPicMapper.findById(hot.getPropicId());
            ProductInfo pro1 = productMapper.selectByPrimaryKey(hot.getPro1());
            ProductInfo pro2 = productMapper.selectByPrimaryKey(hot.getPro2());
            List<ProductInfo> data = new ArrayList<ProductInfo>();
            data.add(pro1);
            data.add(pro2);
            result.add(new HotProductVO(hot.getId(),proPic,data));
        }
        return result;
    }

    public void deleteHotProduct(Integer id) {
        productMapper.deleteHotProduct(id);
    }

    public void insertHotProduct(HotProduct product) {
        productMapper.insertHotProduct(product);
    }

    public SourceInfo getSourceInfo(String traceCode) {
        ProductInfoExample example = new ProductInfoExample();
        ProductInfoExample.Criteria criteria = example.createCriteria();
        criteria.andTracecodeEqualTo(traceCode);
        List<ProductInfo> products = productMapper.selectByExample(example);
        if(products.size() < 1){
            return null;
        }
        ProductInfo product = products.get(0);
        //生猪信息
        PurchaseInfo pig = purchaseInfoMapper.selectByPrimaryKey(product.getAnimalid());

        //检验检疫信息
        ProInspectInfoExample proInspectInfoExample = new ProInspectInfoExample();
        ProInspectInfoExample.Criteria proInspectInfoCriteria = proInspectInfoExample.createCriteria();
        proInspectInfoCriteria.andPurcidEqualTo(pig.getGid());
        List<ProInspectInfo> proInspectInfos = proInspectInfoMapper.selectByExample(proInspectInfoExample);
        ProInspectInfo inspect = null;
        if(proInspectInfos != null && proInspectInfos.size() >= 1){
            inspect = proInspectInfos.get(0);
        }

        //疾病信息
        IllnessprocessExample illExample = new IllnessprocessExample();
        IllnessprocessExample.Criteria illExampleCriteria = illExample.createCriteria();
        illExampleCriteria.andGidEqualTo(pig.getGid());
        List<Illnessprocess> illnessprocesses = illnessprocessMapper.selectByExample(illExample);
        Illnessprocess ill = null;
        if(illnessprocesses != null && illnessprocesses.size() >= 1){
            ill = illnessprocesses.get(0);
        }

        //商品饲养信息集合
        FeedingInfo productFeed = new FeedingInfo();

        FeedingInforExample feedingInforExample = new FeedingInforExample();
        FeedingInforExample.Criteria criteria1 = feedingInforExample.createCriteria();
        criteria.andAnimalidEqualTo(pig.getGid());
        List<FeedingInfor> feedInfors = feedingInforMapper.selectByExample(feedingInforExample);
        //饲养信息
        FeedingInfor feedinfo = null;
        if(feedInfors == null && feedInfors.size() < 1){
            return new SourceInfo(inspect,ill,null);
        }
        feedinfo = feedInfors.get(0);
        //饲料信息
        Feeds feeds = feedsMapper.selectByPrimaryKey(feedinfo.getFeedid());

        productFeed.setFeedName(feeds.getFeedname());//饲料信息
        productFeed.setFeedCount(feedinfo.getFoodamount()+"");//饲养量
        productFeed.setMain(feeds.getComponent());//主要成分
        CompanyInfo company = companyMapper.selectByPrimaryKey(pig.getCompid());
        productFeed.setCompanyName(company.getCompname());

        return new SourceInfo(inspect,ill,productFeed);
    }



    /**
     * 溯源商城后台添加推荐商品
     * @param id
     * @return
     */
    public Integer addRecommProduct(Integer id) {
        Integer num = productMapper.getRecommProductNumber();
        if(num <4){
            productMapper.addRecommproduct(id);
            return 1;
        }
        return 0;
    }


    /**
     * 溯源商城后台查找所有推荐商品
     * @return
     */
    public List<RecommProduct> findAllRecommProduct() {
        return productMapper.findAllRecommproduct();
    }


    /**
     * 溯源商城移除推荐商品
     * @param id
     * @return
     */
    public Integer removeRecommProduct(Integer id) {
        productMapper.deleteRecommProduct(id);
        return 2;
    }

    /**
     * 检验推荐商品是否合法
     * @param id
     * @return
     */
    public Integer checkInfo(Integer id) {
        Integer num = productMapper.getRecommProductNumber();
        ProductInfo productInfo = productMapper.selectByPrimaryKey(id);
        if(productInfo == null){
            return 1;
        }
        if(num <4 ){
            //代表可以添加推荐商品
            return 3;
        }else{
            //1代表推荐栏满了，最多四个
            return 2;
        }
    }


}
